<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item label="用户昵称">
          <el-input
            v-model="listQuery.nickname"
            placeholder="请输入用户昵称"
          ></el-input>
        </el-form-item>

        <el-form-item label="发布日期">
          <el-date-picker
            v-model="listQuery.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="帖子类型">
          <el-select
            v-model="listQuery.category_id"
            placeholder="请选择帖子类型"
          >
            <el-option
              v-for="item in invitationOptions"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search" size="small"
            >查询</el-button
          >
          <el-button @click="reset" size="small">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="downloadLoading"
            type="success"
            icon="el-icon-download"
            @click="handleDownload"
            size="small"
          >
            数据导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      key="id"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
    >
      <el-table-column label="序号" type="index" width="50"> </el-table-column>

      <el-table-column
        label="用户昵称"
        prop="user.nickname"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column label="帖子标题" prop="title" show-overflow-tooltip>
      </el-table-column>

      <el-table-column label="帖子类型" prop="category"> </el-table-column>

      <el-table-column label="浏览" prop="page_view"> </el-table-column>

      <el-table-column label="评论" prop="comment_count"> </el-table-column>
      <el-table-column
        label="发布日期"
        prop="create_time"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="展示状态">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.is_show"
            :active-value="1"
            :inactive-value="0"
            @change="setIsShow($event, row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="{ row, $index }">
          <el-button type="text" size="mini" @click="handleView(row)">
            查看
          </el-button>
          <el-button
            style="margin-left: 10px"
            slot="reference"
            class="red"
            type="text"
            size="mini"
            @click="handleRemove(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList, exportExcel, setIsShow, remove } from "@/api/community";

import Pagination from "@/components/Pagination";
import { invitationOptions } from "@/const";

export default {
  name: "invitation",
  components: { Pagination },
  data() {
    return {
      invitationOptions,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        nickname: undefined,
        date: undefined,
        category_id: 0,
      },
      downloadLoading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      const res = await fetchList(this.listQuery);
      if (res.data) {
        this.list = res.data.list;
        this.total = res.data.pageParam.total;
      }
      this.listLoading = false;
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    reset() {
      this.listQuery = {
        page: 1,
        pageSize: 20,
        nickname: undefined,
        date: undefined,
        category_id: 0,
      };
      this.getList();
    },
    // 查看资料
    handleView(row) {
      this.$router.push({
        path: "/community/invitation/info",
        query: { id: row.id },
      });
    },
    // 数据导出
    async handleDownload() {
      this.downloadLoading = true;
      const res = await exportExcel(this.listQuery);
      if (res) {
        this.downloadFile(res, "帖子列表");
      }
      this.downloadLoading = false;
    },

    async setIsShow(is_show, row) {
      let postData = {
        id: row.id,
        is_show,
      };
      const res = await setIsShow(postData);
      if (res) {
        this.$message({
          message: res.msg,
          type: "success",
        });
        this.getList();
      }
    },

    /**
     * row 当前行数据
     * 删除数据
     */
    async handleRemove(row) {
      this.$confirm("确定删除吗？", "提示").then(async () => {
        const res = await remove({ id: row.id });
        if (res) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.getList();
        }
      });
    },
  },
};
</script>
